package com.laikan.reader.core.utils;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

@SuppressLint ("SimpleDateFormat")
public class StringUtil {

    // 时间样式
    // 凌晨,上午,中午,下午,晚上,昨天,月-日_分:秒
    @SuppressWarnings ("deprecation")
    public static String showTime(String str1) {
        if (str1 == null || "".equals(str1)) {
            return null;
        }
        long s = 0L;
        try {
            s = Long.parseLong(str1);
        }catch (Exception e) {
            return null;
        }

        Date date = new Date(s);
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        DateFormat df;

        // 没有隔天
        if (Long.parseLong(str1) > c.getTimeInMillis()) {
            // 只能定义为float或者double
            float diffSecond = (System.currentTimeMillis() - date.getTime()) / 1000;
            if (diffSecond <= 10) {
                return 10 + "秒前";
            }else if (diffSecond <= 50) {
                return (int)Math.ceil(diffSecond / 10) * 10 + "秒前";
            }else if (diffSecond < 3600) {// 一小时内
                return (int)Math.ceil(diffSecond / 60) + "分钟前";
            }else if (diffSecond < 60 * 60 * 24) {// 10小
                df = new SimpleDateFormat("HH:mm");
                String str = "";
                if (date.getTime() - c.getTimeInMillis() < 6 * 3600 * 1000) {
                    str = "凌晨";
                }else if (date.getTime() - c.getTimeInMillis() < 12 * 3600 * 1000) {
                    str = "上午";
                }else if (date.getTime() - c.getTimeInMillis() < 14 * 3600 * 1000) {
                    str = "中午";
                }else if (date.getTime() - c.getTimeInMillis() < 18 * 3600 * 1000) {
                    str = "下午";
                }else {
                    str = "晚上";
                }
                return str + df.format(date);
            }
        }
        // 昨天
        c.add(Calendar.DAY_OF_MONTH, -1);
        if (date.getTime() > c.getTimeInMillis()) {
            df = new SimpleDateFormat("HH:mm");
            return "昨天" + df.format(date);
        }

        if (c.get(Calendar.YEAR) == 1900 + date.getYear()) {
            df = new SimpleDateFormat("MM-dd HH:mm");
        }else {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        }
        return df.format(date);
    }

    /**
     * 刚刚 12:08 03-21
     *
     * @param date
     *
     * @return
     */
    @SuppressWarnings ("deprecation")
    public static String showTimeShort(Date date) {

        if (date == null) {
            return null;
        }

        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        DateFormat df;
        // 没有隔天
        if (date.getTime() > c.getTimeInMillis()) {
            // 只能定义为float或者double
            float diffSecond = (System.currentTimeMillis() - date.getTime()) / 1000;
            if (diffSecond <= 10) {
                return 10 + "秒前";
            }else if (diffSecond <= 50) {
                return (int)Math.ceil(diffSecond / 10) * 10 + "秒前";
            }else if (diffSecond < 3600) {// 一小时内
                return (int)Math.ceil(diffSecond / 60) + "分钟前";
            }else if (diffSecond < 60 * 60 * 24) {// 10小
                df = new SimpleDateFormat("HH:mm");
                String str = "";
                if (date.getTime() - c.getTimeInMillis() < 6 * 3600 * 1000) {
                    str = "凌晨";
                }else if (date.getTime() - c.getTimeInMillis() < 12 * 3600 * 1000) {
                    str = "上午";
                }else if (date.getTime() - c.getTimeInMillis() < 14 * 3600 * 1000) {
                    str = "中午";
                }else if (date.getTime() - c.getTimeInMillis() < 18 * 3600 * 1000) {
                    str = "下午";
                }else {
                    str = "晚上";
                }
                return str + df.format(date);
            }
        }
        // 昨天
        c.add(Calendar.DAY_OF_MONTH, -1);
        if (date.getTime() > c.getTimeInMillis()) {
            df = new SimpleDateFormat("HH:mm");
            return "昨天";// + df.format(date);
        }

        if (c.get(Calendar.YEAR) == 1900 + date.getYear()) {
            df = new SimpleDateFormat("MM-dd");
        }else {
            df = new SimpleDateFormat("yyyy-MM-dd");
        }

        return df.format(date);

    }

    /**
     * 计算距离当前的时间
     *
     * @param millisecond
     *
     * @return
     */
    public static String diffTime(Long millisecond) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(new Date(millisecond));

        String before = "";

        // 1.计算距离当前时间的天数
        Long currentTime = System.currentTimeMillis();
        Long updateTime = new Long(millisecond);
        Long diff = currentTime - updateTime;

		/*System.out.println("年:"+diff / 1000 / 60 / 60 / 24 / 30 / 12);
        System.out.println("月:"+diff / 1000 / 60 / 60 / 24 / 30);
		System.out.println("日:"+diff / 1000 / 60 / 60 / 24);
		System.out.println("时:"+diff / 1000 / 60 / 60);
		System.out.println("分:"+diff / 1000 / 60);
		System.out.println("秒:"+diff / 1000);*/

        if (diff / 1000 <= 60) {
            Long second = diff / 1000;
            before = second + "秒";
            return before;

        }else if (diff / 1000 / 60 < 60) {
            Long min = diff / 1000 / 60;
            before = min + "分";
            return before;

        }else if (diff / 1000 / 60 / 60 < 24) {
            Long hours = diff / 1000 / 60 / 60;
            before = hours + "小时";
            return before;

        }else if (diff / 1000 / 60 / 60 / 24 < 30) {
            Long days = diff / 1000 / 60 / 60 / 24;
            before = days + "天";
            return before;

        }else if (diff / 1000 / 60 / 60 / 24 / 30 < 12) {
            Long month = diff / 1000 / 60 / 60 / 24 / 30;
            before = month + "个月";
            return before;

        }else {
            Long year = diff / 1000 / 60 / 60 / 24 / 30 / 12;
            before = year + "年";
            return before;
        }
    }

    /**
     * 通过毫秒值计算出时间:按 年/天/时/分/秒 显示
     *
     * @param millisecond
     *
     * @return
     */
    public static String diffTime2(Long millisecond) {

        String before = "";

        // 1.该毫秒值有几天
        Long diff = millisecond;

        if (diff / 1000 <= 60) {
            Long second = diff / 1000;
            before = second + "秒";
            return before;

        }else if (diff / 1000 / 60 < 60) {
            Long min = diff / 1000 / 60;
            before = min + "分";
            return before;

        }else if (diff / 1000 / 60 / 60 < 24) {
            Long hours = diff / 1000 / 60 / 60;
            before = hours + "小时";
            return before;

        }else if (diff / 1000 / 60 / 60 / 24 < 30) {
            Long days = diff / 1000 / 60 / 60 / 24;
            before = days + "天";
            return before;

        }else if (diff / 1000 / 60 / 60 / 24 / 30 < 12) {
            Long month = diff / 1000 / 60 / 60 / 24 / 30;
            before = month + "个月";
            return before;

        }else {
            Long year = diff / 1000 / 60 / 60 / 24 / 30 / 12;
            before = year + "年";
            return before;
        }
    }
}
